package servlet.client;

import service.ClientService;
import service.OrderService;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Objects;

/**
 * @author gzb
 */
@WebServlet(name = "BookReturnServlet", value = "/BookReturnServlet")
public class ClientReturnBookServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String number = request.getParameter("number");
        int id = 0;
        if (!Objects.equals(number, "")) {
            id = Integer.parseInt(number);
        }
        System.out.println("1");
        System.out.println(id);
        String msg;
        try {
            ClientService.returnBook(OrderService.selectById(id));
            msg = "{\"msg\":\"success\"}";
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        response.getWriter().write(msg);
        System.out.println("finish");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("BookReturnServlet现在来了一个post请求，将要去走get的代码了。");
        doGet(request, response);
    }
}
